#include <iostream>
#include <cmath>
#include "main/utils.h"
using namespace std;

double powerWithUnsignedExpRecursive(double base, unsigned int exp) {
  if (exp == 0) return 0;
  if (exp == 1) return base;
  double ret = powerWithUnsignedExpRecursive(base, exp >> 1);
  ret *= ret;
  if (exp & 1) {
    ret *= base;
  }
  return ret;
}

double powerWithUnsignedExp(double base, unsigned int exp) {
  double ret = 1;
  for (int i = 0; i < exp; ++i) {
    ret *= base;
  }
  return ret;
}

double power(double base, int exp) {
  if (isFloatEqual(base, 0) && exp < 0) {
    return 0;
  }
  double abs_exp = exp < 0 ? -exp : exp;
  double ret = powerWithUnsignedExpRecursive(base, abs_exp);
  if (exp < 0) {
    ret = 1 / ret;
  }
  return ret;
}

int main() {
  cout << "base exp result expected" << endl;
  cout << "2 3 " << power(2, 3) << " 8" << endl;
  cout << "-2 3 " << power(-2, 3) << " -8" << endl;
  cout << "2 -3 " << power(2, -3) << " 0.125" << endl;
  cout << "2 0 " << power(2, 0) << " 1" << endl;
  cout << "0 0 " << power(0, 0) << " 1" << endl;
  cout << "0 4 " << power(0, 4) << " 0" << endl;
  cout << "0 -4 " << power(0, -4) << " 0" << endl;
}
